package com.demo.function.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * spring异步线程
 * @author K
 */
@Slf4j
@Service
public class SpringThread {

    /**
     * 异步线程返回结果
     * @param i 参数
     * @return 结果
     */
    @Async("taskExecutor")
    public Future<String> getThreadResult(int i) {
        String str = "getThreadResult, i = " + i;
        log.info("线程：{}， i = {}", Thread.currentThread().getName(), i);
        return new AsyncResult<>(str);
    }

    /**
     * 异步线程，没有返回值
     * 该线程池使用拒绝策略模式，超过线程池数量，现在线程不做处理，也不抛异常
     * @param i 参数
     * @throws InterruptedException 异常
     */
    @Async("taskDiscardExecutor")
    public void taskDiscard(int i) throws InterruptedException {
        log.info("线程：{}， i = {}", Thread.currentThread().getName(), i);
        Thread.sleep(1000);
    }
}
